//
//  MXButton.swift
//  MXButton_Test
//
//  Created by 陈学明 on 2018/2/8.
//  Copyright © 2018年 sinaAging. All rights reserved.
//
/*
 封装环境，真特么不想说什么，UI一定要设计按钮图片和文本各种布局，这就算了，自己自定义一个View做button，但是呢UI又要求按钮需要触摸变暗的效果，我勒个fuck，不生气😓，作为一个iOS开发只好满足UI妹妹，这一点点委屈又算得了什么呢，自定义个button继承自UIButton
 */
/**自定义按钮封装
 *1.属性封装支持UIButton所有状态
 *2.对按钮的布局重新更改
 *3.只支持单行文本和图片
 */
enum MXButtonLayeoutType {
    //图片在左边面，文字在右边
    case ImageToLeft
    //图片在右面，文字在左边
    case ImageToRight
    //图片在上面，文字在下边
    case ImageToTop
    //图片在下面，文字在上边
    case ImageToBottom
}

import UIKit

class MXButton: UIButton {
    /** 事件回调 */
    public var action:((MXButton)->())?
    /** 布局风格 默认 ImageToLeft */
    public var type:MXButtonLayeoutType = .ImageToLeft
    /** 图片离文本的高度 */
    public var imageToTitleSize: CGFloat = 4.0
    /** 图片显示大小 */
    public var imageSize:CGSize = CGSize(width: 50, height: 50)
    /** 文本字体大小 */
    public var textfont:UIFont = UIFont.systemFont(ofSize: 15)

    /** 重新布局
     */
    override func layoutSubviews() {
        titleLabel?.font = textfont
        titleLabel?.textAlignment = .center
        super.layoutSubviews()
    }
    /** 重写imageRect方法
     */
    override func imageRect(forContentRect contentRect: CGRect) -> CGRect {
        return drawImageRact(contentRect)
    }
    /** 重写titleRect方法
     */
    override func titleRect(forContentRect contentRect: CGRect) -> CGRect {
        return drawTitleRact(contentRect)
    }
}
//MARK: -外部接口
extension MXButton {
    public class func button(frame:CGRect,imageSize:CGSize?, type:MXButtonLayeoutType) -> MXButton {
        let btn = MXButton(type:.custom)
        btn.frame = frame
        btn.titleLabel?.font = btn.textfont
        btn.titleLabel?.numberOfLines = 0
        btn.type = type
        btn.imageSize = imageSize ?? CGSize.zero
        btn.addTarget(btn, action: #selector(clicked), for: .touchUpInside)
        
        return btn
    }
    public func image(nor: UIImage?,select: UIImage?) {
        setImage(nor, for: .normal)
        setImage(select, for: .selected)
    }
    public func title(nor: String?,select: String?) {
        setTitle(nor, for: .normal)
        setTitle(select, for: .selected)
    }
    public func titleColor(nor: UIColor?,select: UIColor?) {
        setTitleColor(nor, for: .normal)
        setTitleColor(select, for: .selected)
    }
    public func backgroundImage(nor: UIImage?, select: UIImage?) {
        setBackgroundImage(nor, for: .normal)
        setBackgroundImage(select, for: .selected)
    }
}
//MARK: -action
extension MXButton {
    @objc func clicked() {
        isSelected = !isSelected
        if let action = self.action {
            action(self)
        }
    }
}
//MARK: -绘制图片和文字大小
extension MXButton {
   private func drawImageRact(_ contentRect: CGRect) -> CGRect {
        var y: CGFloat = 0
        var x: CGFloat = 0
        let textSize = ((currentTitle ?? "") as NSString).boundingRect(with: CGSize(width: frame.size.width, height: frame.size.height), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: [NSAttributedStringKey.font: textfont], context: nil).size
        switch type {
            case .ImageToLeft:
                y = (frame.size.height - imageSize.height)/2.0
                x = (frame.size.width - imageSize.width - imageToTitleSize - textSize.width)/2.0
            case .ImageToRight:
                y = (frame.size.height - imageSize.height)/2.0
                x = (frame.size.width - imageSize.width - imageToTitleSize - textSize.width)/2.0 +  imageToTitleSize + textSize.width
            case .ImageToTop:
                y = (frame.size.height - imageSize.height - imageToTitleSize - textSize.height)/2.0
                x = (frame.size.width - imageSize.width)/2.0
            case .ImageToBottom:
                y = (frame.size.height - imageSize.height - imageToTitleSize - textSize.height)/2.0 + textSize.height + imageToTitleSize
                x = (frame.size.width - imageSize.width)/2.0
        }
        return CGRect(x: x, y: y, width: imageSize.width, height: imageSize.height)
    }
    
   private func drawTitleRact(_ contentRect: CGRect) -> CGRect {
  
        let textSize = ((currentTitle ?? "") as NSString).boundingRect(with: CGSize(width: frame.size.width, height: frame.size.height), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: [NSAttributedStringKey.font: textfont], context: nil).size
        var y: CGFloat = 0
        var x: CGFloat = 0
        let w = textSize.width
        let h = textSize.height
        switch type {
        case .ImageToLeft:
            y = (frame.size.height - h)/2.0
            x = (frame.size.width - imageSize.width - imageToTitleSize - w)/2.0 + imageToTitleSize + imageSize.width
        case .ImageToRight:
            y = (frame.size.height - h)/2.0
            x = (frame.size.width - imageSize.width - imageToTitleSize - w)/2.0
        case .ImageToTop:
            y = (frame.size.height - imageSize.height - imageToTitleSize - h)/2.0 + imageToTitleSize + imageSize.height
            x = (frame.size.width - w)/2.0
        case .ImageToBottom:
            y = (frame.size.height - imageSize.height - imageToTitleSize - h)/2.0
            x = (frame.size.width - w)/2.0
        }
        return CGRect(x: x, y: y, width: w, height: h)
    }
}

